<?php

require_once ('config.php');

$friends = $facebook->api_client->users_getinfo($facebook->api_client->friends_get(), 'birthday');

?>

<h1>Birthday Books</h1>

<style type="text/css">
	<?php require('style.css'); ?>
</style>

<table>

<?php

$now = strtotime(date("jS") . date("F"));

$with_birthday = array();
$without_birthday = array();

foreach ($friends as $friend) {
	$person = array();
	$person['name'] = '<fb:name uid="' . $friend[uid] . '"/>';
	$person['image'] = '<fb:profile-pic uid="' . $friend[uid] . '" linked="true" size="square"/>';
	$person['day'] = date("jS", strtotime($friend[birthday]));
	$person['month'] = date("F", strtotime($friend[birthday]));
	$person['absolute_timestamp'] = strtotime($friend[birthday]);
	$person['relative_timestamp'] = strtotime($person['month'] . $person['day']);

	if ($person['relative_timestamp'] < $now)
	{
		// past
		$person['year'] = date('Y', strtotime('+1 year'));
	}
	else
	{
		// future
		$person['year'] = date('Y');
	}

	$person['relative_timestamp_with_year'] = strtotime($person['month'] . $person['day'] . $person['year']);

	if ($person['absolute_timestamp'])
	{
		$with_birthday[] = $person;
	}
	else
	{
		$without_birthday[] = $person;
	}
}

foreach ($with_birthday as $key => $row)
{
	$relative_timestamp_with_year[$key] = $row['relative_timestamp_with_year'];
}

array_multisort($relative_timestamp_with_year, SORT_ASC, $with_birthday);

?>

<table>

<?php

$i = 0;
foreach ($with_birthday as $friend)
{
	echo '<tr class="vevent';
	if ($i == 0)
	{
		echo ' odd';
		$i = 1;
	}
	else
	{
		$i = 0;
	}
	echo '">';
	echo '<td>' . $friend['image'] . '</td>';
	echo '<th scope="row" class="summary vcard"><span class="fn">' . $friend['name'] . '</span></th>';
	echo '<td><abbr class="dtstart" title="' . $friend['year'] . '-' . date('m', strtotime($friend['month'])) . '-' . substr($friend['day'],0,-2) . '">' . $friend['day'] . ' ' . $friend['month'] . ' ' . $friend['year'] . '</td>';
	echo '</tr>';
}

?>

</table>

<fb:google-analytics uacct="UA-21651067-1" page="Birthdays Book" />
